﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Models;

namespace WebApi.Bill.Biz
{
    public class BizMeeting : BizBase
    {
        readonly string tb = "TbMeeting";
        readonly string pk = "pkCode";



        public MeetingModel Get(int code)
        {
            return Db.GetModel<MeetingModel>(x => x.Code == code);

        }

        public void Update(MeetingModel model)
        {
            Db.UpdateModel<MeetingModel>(model);
        }

        public int Add(MeetingModel model)
        {
            return Db.InsertModel<MeetingModel>(model);
        }

        #region 获取当天会议（大于等于当前时间）
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="pageNum">当前页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="recordCount">总列数</param>
        /// <returns></returns>
        public List<MeetingModel> GetPageList(dynamic data, ref int recordCount, ref int totalPage)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"select * from {this.tb} where 1=1 and   DATE_FORMAT(tEnd,'%H:%m:%s') >= DATE_FORMAT( NOW(),'%H:%m:%s')    "); // and  DATE_FORMAT( NOW(),'%H:%m:%s') <=  DATE_FORMAT(tEnd,'%H:%m:%s')
            JObject joFilters = FiltersSql.GetQueryCond(data);
            sb.Append(joFilters["sqlAppend"].ToString());
            sb.Append($" and  DATE_FORMAT(tDay, '%Y-%m-%d') = '{DateTime.Now.ToString("yyyy-MM-dd")}'");
            sb.Append($" order by tCreate");
            int pageNum = Convert.ToInt32(joFilters["pageNum"]);
            int pageSize = Convert.ToInt32(joFilters["pageSize"]);
            return Da.PageList<MeetingModel>(sb.ToString(), pageNum, pageSize, ref recordCount, ref totalPage);
        }
        #endregion

        #region 获取当前会议信息
        public dynamic GetCurrentMeeting(string sMeetingRoom)
        {
            StringBuilder sb = new StringBuilder();
            //sb.Append($"select * from {this.tb} where 1=1 and sRoomName = '{sMeetingRoom}' AND  UNIX_TIMESTAMP( NOW())>= UNIX_TIMESTAMP(tStart)   AND  UNIX_TIMESTAMP( NOW())<= UNIX_TIMESTAMP(tEnd) AND  DATE_FORMAT(tDay, '%Y-%m-%d') = '{DateTime.Now.ToString("yyyy-MM-dd")}' ");
            sb.Append($"select * from {this.tb} where 1=1 and sRoomName = '{sMeetingRoom}' AND  UNIX_TIMESTAMP( NOW())>= UNIX_TIMESTAMP(tStart)   AND  UNIX_TIMESTAMP( NOW())<= UNIX_TIMESTAMP(tEnd) ");
            MeetingModel meetingModel = Da.GetModel<MeetingModel>(sb.ToString());
            if (meetingModel!=null)
            {
                return new { SMeetingName = meetingModel.SMeetingName, SBookMan = meetingModel.SBookMan, STime = $"{meetingModel.TStart.ToString("HH:mm")}-{meetingModel.TEnd.ToString("HH:mm")}", SDay = meetingModel.TDay.ToString("yyyy-MM-dd"),MeetingState = "正在使用中", SOriginId = meetingModel.SOriginId };
            }
            else
            {
                return new { SMeetingName = "暂无会议", SBookMan = "暂无会议", STime = $"", SDay = "", MeetingState = "空闲中", SOriginId = "" };
            }
           
        }
        #endregion


    }
}
